package com.wzy.mall.service.client.impl;

import com.wzy.mall.mapper.clientMapper.GoodsMapper;
import com.wzy.mall.model.bo.clientBO.GoodsAskBO;
import com.wzy.mall.model.pojo.clientPOJO.ReplyPOJO;
import com.wzy.mall.model.vo.clientVO.*;
import com.wzy.mall.service.client.GoodsService;
import com.wzy.mall.utils.HTTPUtils;
import com.wzy.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.text.DecimalFormat;
import java.util.List;

public class GoodsServiceImpl implements GoodsService {

    public List<IndexGoodsVO> getGoodsByType(int typeId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<IndexGoodsVO> indexGoodsVOList = null;
        try {
            indexGoodsVOList = mapper.getGoodsByType(typeId);
            if(indexGoodsVOList != null) {
                String imageBaseURL = HTTPUtils.getImageBaseURL();
                for (IndexGoodsVO indexGoodsVO : indexGoodsVOList) {
                    String img = indexGoodsVO.getImg();
                    indexGoodsVO.setImg(imageBaseURL + img);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return indexGoodsVOList;
    }

    public GoodsInfoVO getGoodsInfo(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        GoodsInfoVO goodsInfoVO = null;
        try {
            goodsInfoVO = mapper.selectGoods(id);
            // 拼接图片地址
            if(goodsInfoVO != null) {
                String imageBaseURL = HTTPUtils.getImageBaseURL();
                goodsInfoVO.setImg(imageBaseURL + goodsInfoVO.getImg());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return goodsInfoVO;
    }

    public GoodsCommentListVO getGoodsComment(int goodsId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        GoodsCommentListVO goodsCommentListVO = null;
        try {
            List<GoodsCommentVO> goodsCommentVOS = mapper.selectCommentByGoodsID(goodsId);

            if(goodsCommentVOS != null) {
                goodsCommentListVO = new GoodsCommentListVO();
                goodsCommentListVO.setCommentList(goodsCommentVOS);
                // 计算好评率
                double favouriteCount = 0.0;
                for (GoodsCommentVO goodsCommentVO : goodsCommentVOS) {
                    if(goodsCommentVO.getScore() == 100) {
                        favouriteCount++;
                    }
                }
                //System.out.println("好评率：" + (favouriteCount / goodsCommentVOS.size()) * 100);
                // 好评率保留两位小数
                Double rate = (favouriteCount / goodsCommentVOS.size()) * 100;
                DecimalFormat df = new DecimalFormat("#.##");
                rate = Double.parseDouble(df.format(rate));
                goodsCommentListVO.setRate(rate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return goodsCommentListVO;
    }

    public int askGoodsMsg(GoodsAskBO goodsAskBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        int result = 0;
        try {
            // 根据商品ID获取商品名
            // 根据昵称获取用户ID
            ReplyPOJO replyPOJO = mapper.selectGoodsAndUser(goodsAskBO);
            System.out.println(replyPOJO);

            if(replyPOJO != null) {
                // 将留言封装进POJO对象
                replyPOJO.setUserNickname(goodsAskBO.getToken());
                replyPOJO.setGoodsId(goodsAskBO.getGoodsId());
                replyPOJO.setContent(goodsAskBO.getMsg());
                replyPOJO.setState(1);
                result = mapper.insertReply(replyPOJO);

                sqlSession.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return result;
    }

    public List<ReplyGetVO> getGoodsMsg(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<ReplyGetVO> replyGetVOList = null;
        try {
            replyGetVOList = mapper.selectReply(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return replyGetVOList;
    }

    public List<GoodsSearchVO> searchGoods(String keyword) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<GoodsSearchVO> goodsSearchVOList = null;
        try {
            goodsSearchVOList = mapper.selectByGoodsname(keyword);
            if (goodsSearchVOList != null) {
                // 处理图片路径
                for (GoodsSearchVO goodsSearchVO : goodsSearchVOList) {
                    String imageBaseURL = HTTPUtils.getImageBaseURL();
                    goodsSearchVO.setImg(imageBaseURL + goodsSearchVO.getImg());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return goodsSearchVOList;
    }

}
